package com.k.demo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.UserAddress;
import com.k.demo.service.UserAddressService;

@RestController
@RequestMapping("/useraddress")
public class UserAddressController {

	@Autowired
	private UserAddressService userAddressService;

	@RequestMapping("/all")
	public Object all() {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("AllList", userAddressService.getALL());

		return map;
	}

	// 当前用户下的
	@RequestMapping("/getaddresslist")
	public Object getNowAll(HttpSession session) {

		System.out.println("根据id查找");

		Map<String, Object> map = new HashMap<String, Object>();

		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");

		int userid = customerUser.getUser_id();

		System.out.println("当前用户userid" + userid);

		map.put("AddressList", userAddressService.getNowAddresses(userid));

		return map;
	}

	@RequestMapping("/addaddress")
	public Object addAddress(@RequestBody UserAddress userAddress, HttpSession session) {

		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");

		userAddress.setUser_id(customerUser.getUser_id());

		System.out.println("当前用户收货人联系方式:" + userAddress.toString());
		
		int userid = customerUser.getUser_id();
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(userAddress.getDefult_flag()==1) {
			
			System.out.println("当前保存地址变成为默认地址");
			
			userAddressService.updatedefult(userid);
			
			map.put("addResult", userAddressService.addUserAddress(userAddress));
			
		}else {
			
			System.out.println("当前保存地址为非默认地址");
			
			map.put("addResult", userAddressService.addUserAddress(userAddress));
		}

		return map;
	}

	@RequestMapping("/deleteaddress/{address_id}")
	public Object falseDeleteAddress(@PathVariable("address_id") int address_id) {

		// 删除用户地址

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("addressList", userAddressService.falseDeleteUserAddress(address_id));

		return map;
	}

	// 获取当前用户地址数量
	@RequestMapping("/countaddress")
	public Object countUserAddress(HttpSession session) {

		Map<String, Object> map = new HashMap<String, Object>();

		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");

		int userid = customerUser.getUser_id();

		map.put("count", userAddressService.countUserAddress(userid));

		return map;

	}

	// 修改用户的某个地址
	@RequestMapping("/modifyaddress")

	public Object modifyAddress(@RequestBody UserAddress userAddress,HttpSession session) {
		
		System.out.println("当前用户收货人联系方式:"+userAddress.toString());
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");
		
		int userid = customerUser.getUser_id();
		
		
		if(userAddress.getDefult_flag()==1) {
			
			System.out.println("当前修改地址变成为默认地址");
			
			userAddressService.updatedefult(userid);
			
			map.put("addressList", userAddressService.modifyUserAddress(userAddress));
			
			
		}else {
			
			System.out.println("当前修改地址为非默认地址");
			
			map.put("addressList", userAddressService.modifyUserAddress(userAddress));
			
		}
		
		return map;
		
	}

}
